package com.benjaminwan.read.free.base.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.benjaminwan.read.free.base.adapter.MvRxEpoxyController
import com.benjaminwan.read.free.databinding.FragmentDialogListBinding

abstract class BaseDialogListFragment : BaseDialogFragment<FragmentDialogListBinding>() {

    open val itemDecoration: RecyclerView.ItemDecoration? = null

    private val epoxyController by lazy { epoxyController() }

    val negative: Button
        get() = binding.negativeBtn

    val positive: Button
        get() = binding.positiveBtn

    val title: TextView
        get() = binding.titleTv

    val icon: ImageView
        get() = binding.iconImage

    override lateinit var binding: FragmentDialogListBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentDialogListBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        epoxyController.onRestoreInstanceState(savedInstanceState)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        epoxyController.onSaveInstanceState(outState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }

    private fun initViews() {
        binding.commonListRv.setHasFixedSize(false)
        val decoration = itemDecoration
        decoration?.let {
            binding.commonListRv.addItemDecoration(decoration)
        }
        binding.commonListRv.setController(epoxyController)
    }

    override fun invalidate() {
        binding.commonListRv.requestModelBuild()
    }

    abstract fun epoxyController(): MvRxEpoxyController

}